package com.qs.education.bean.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.qs.education.bean.dao.IEducationQuestionDao;
import com.qs.education.bean.entity.EducationQuestion;

@Repository
public class EducationQuestionDaoImpl extends BaseDaoImpl<EducationQuestion> implements IEducationQuestionDao {

	@Override
	public int getQuestionCount(EducationQuestion question) {

		String jql = "Select COUNT(*)  from EducationQuestion eq where 1 = 1";
		Map<String, Object> params = new HashMap<>();
		if (question.getLesson() != null && question.getLesson().getUnit() != null
				&& question.getLesson().getUnit().getGrade() != null
				&& question.getLesson().getUnit().getGrade().getId() != null) {
			jql += " and eq.lesson.unit.grade.id = :gradeId";
			params.put("gradeId", question.getLesson().getUnit().getGrade().getId());
		}
		if (question.getLesson() != null && question.getLesson().getUnit() != null
				&& question.getLesson().getUnit().getSubject() != null
				&& question.getLesson().getUnit().getSubject().getId() != null) {
			jql += " and eq.lesson.unit.subject.id = :subjectId";
			params.put("subjectId", question.getLesson().getUnit().getSubject().getId());
		}
		if (question.getType() != null && question.getType()!="") {
			jql += " and eq.type = :type";
			params.put("type", question.getType());
		}
		if (question.getLesson() != null && question.getLesson().getUnit() != null
				&& question.getLesson().getUnit().getId() != null) {
			jql += " and eq.lesson.unit.id = :unitId";
			params.put("unitId", question.getLesson().getUnit().getId());
		}
		if (question.getLesson() != null && question.getLesson().getId() != null) {
			jql += " and eq.lesson.id = :lessonId";
			params.put("lessonId", question.getLesson().getId());
		}
		if (question.getDescription() != null) {
			jql += " and eq.description like :desc";
			params.put("desc", "%" + question.getDescription() + "%");
		}
		List<?> result = query(jql, params);
		if (null != result && !result.isEmpty()) {
			return Integer.parseInt(result.get(0).toString());
		}

		return 0;
	}

	@Override
	public List<EducationQuestion> getQuestionList(EducationQuestion question, Integer currentPage,
			Integer pageSize) {
		
		String jql = "Select eq  from EducationQuestion eq where 1 = 1";
		Map<String, Object> params = new HashMap<>();
		if (question.getLesson() != null && question.getLesson().getUnit() != null
				&& question.getLesson().getUnit().getGrade() != null
				&& question.getLesson().getUnit().getGrade().getId() != null) {
			jql += " and eq.lesson.unit.grade.id = :gradeId";
			params.put("gradeId", question.getLesson().getUnit().getGrade().getId());
		}
		if (question.getLesson() != null && question.getLesson().getUnit() != null
				&& question.getLesson().getUnit().getSubject() != null
				&& question.getLesson().getUnit().getSubject().getId() != null) {
			jql += " and eq.lesson.unit.subject.id = :subjectId";
			params.put("subjectId", question.getLesson().getUnit().getSubject().getId());
		}
		if (question.getType() != null && question.getType()!="") {
			jql += " and eq.type = :type";
			params.put("type", question.getType());
		}
		if (question.getLesson() != null && question.getLesson().getUnit() != null
				&& question.getLesson().getUnit().getId() != null) {
			jql += " and eq.lesson.unit.id = :unitId";
			params.put("unitId", question.getLesson().getUnit().getId());
		}
		if (question.getLesson() != null && question.getLesson().getId() != null) {
			jql += " and eq.lesson.id = :lessonId";
			params.put("lessonId", question.getLesson().getId());
		}
		if (question.getDescription() != null) {
			jql += " and eq.description like :desc";
			params.put("desc", "%" + question.getDescription() + "%");
		}
		int firstResult = (currentPage - 1) * pageSize;
		List<EducationQuestion> list = query(jql,EducationQuestion.class,firstResult,pageSize,params);
		
		return list;
	}

}
